package com.ostrich.note.ui.settings.views;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ostrich.note.R;
import com.ostrich.note.base.BaseMvvmActivity;
import com.ostrich.note.databinding.ActivitySettingsEditBinding;
import com.ostrich.note.entity.BaseResponseEntity;
import com.ostrich.note.ui.note.views.NoteAddActivity;
import com.ostrich.note.ui.settings.viewmodel.SettingsEditViewModel;
import com.ostrich.note.utils.UploadUtils;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

public class SettingsEditActivity extends BaseMvvmActivity<ActivitySettingsEditBinding, SettingsEditViewModel> {

    private static final int REQUEST_CODE_ALBUM = 102;
    private  String path ;
    @Override
    protected void iniView() {

    }

    @Override
    public int getLayout() {
        return R.layout.activity_settings_edit;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        binding.tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String avatar = "";//头像
                String name = binding.etNickname.getText().toString();
                String signature = binding.etSignature.getText().toString();
                if(TextUtils.isEmpty(path)){
                    avatar = SPUtils.getInstance().getString("avatar");
                }else{
                    avatar = path;
                }
                viewModel.noteUserEdit(name,avatar,signature).observe(SettingsEditActivity.this, new Observer<BaseResponseEntity>() {
                    @Override
                    public void onChanged(BaseResponseEntity baseResponseEntity) {
                        if(baseResponseEntity.getCode() == 200){
                            ToastUtils.showShort(baseResponseEntity.getMessage());
                            finish();
                        }
                    }
                });
            }
        });

        binding.tvCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PictureSelector.create(SettingsEditActivity.this,REQUEST_CODE_ALBUM).selectPicture();
            }
        });
        binding.clBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    //打开相机或相册后返回结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_CODE_ALBUM && resultCode == Activity.RESULT_OK){
            if (data != null) {
                PictureBean pictureBean = data.getParcelableExtra(PictureSelector.PICTURE_RESULT);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        path = UploadUtils.upload(pictureBean.getPath());

                    }
                }).start();


            }
        }
    }

    @Override
    public void createViewModel() {
        viewModel = new ViewModelProvider(this).get(SettingsEditViewModel.class);

    }
}